<template>
	<view class="regist">
		<view class="text-title">
			欢迎注册宠物星球
		</view>
		头像：
		<image :src="registData.avaters" mode="" class="avaters" style="width: 200rpx;height: 200rpx;"></image>
		<button open-type="chooseAvatar" type="primary" @chooseavatar="getAvater"> 填入微信头像</button>
		昵称：
		<input type="nickname" class="reginp" placeholder="输入您的昵称" @blur="nameBlur" />
		<view class="sub-title-text mtb-12">昵称、头像可使用微信昵称、头像</view>
		手机号：
		<input type="tel" class="reginp" placeholder="输入您的手机号" :value="registData.phone" @blur="phoneBlur" />
		<view class="sub-title-text mtb-12">手机号务必填写正确、以便买家可以联系您、宠物走失后可以联系到您~</view>
		密码:
		<input type="safe-password" class="reginp" placeholder="设置您的密码" :value="registData.password" @blur="pasBlur" />
		请选择您的身份：
		<radio-group @change="radioChange">
			<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value">
				<view class="flex" style="margin: 12rpx 0;">
					<radio :value="item.value" :checked="index === current" />
					<view>{{item.name}}</view>
				</view>
			</label>
		</radio-group>
		<view class="sub-title-text mtb-12">身份一经选择不可更改，请谨慎选择</view>
		<button type="button" @click="submit" class="regbtn">注册</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				registData: {
					nickname: '',
					avaters: 'https://616e-animal-4g5ks6ct0e7f251e-1315698572.tcb.qcloud.la/logo.png?sign=ffe3e111a4b0466169e3cee81c1a2b4f&t=1721984968',
					phone: null,
					password: null,
					auth: 0,
				},
				items: [{
						value: 0,
						name: '寻宠者（寻找宠物、丢失宠物）',
						checked: 'true'
					},
					{
						value: 0,
						name: '消费者（购买宠物、宠物用品）'
					},
					{
						value: 1,
						name: '店主（宠物行业从业者、出售宠物用品）'
					}
				],
				current: 0
			};
		},
		methods: {
			getAvater(e) {
				// console.log('e', e)
				this.registData.avaters = e.detail.avatarUrl
			},
			nameBlur(e) {
				this.registData.nickname = e.detail.value
			},
			phoneBlur(e) {
				if (this.isValidChinesePhoneNumber(e.detail.value)) {
					this.registData.phone = e.detail.value
				} else {
					uni.showToast({
						title: "手机号不正确",
						icon: "error"
					})
				}
			},
			pasBlur(e) {
				this.registData.password = e.detail.value
			},
			submit() {
				// console.log(this.registData)
				if (this.registData.password && this.registData.phone && this.registData.nickname) {
					uni.showModal({
						content: "请您再次确认信息无误后点击确定",
						success: async (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: "LODDING",
									mask: true
								})
								let fileName = this.registData.avaters.split('/')[this.registData.avaters.split('/').length - 1]
								let resAvater = await this.$clould.uploadFile({
									cloudPath: 'avater/' + fileName,
									filePath: this.registData.avaters
								})
								// console.log("resAvater:", resAvater)
								let registInfo = await wx.cloud.callFunction({
									name: 'logins',
									data: {
										useName: 'regist',
										nickname: this.registData.nickname,
										password: this.registData.password,
										account: this.registData.phone,
										avater: resAvater.fileID,
										auth: this.registData.auth
									}
								})
								if (registInfo.errMsg == "cloud.callFunction:ok") {
									let isReg = await wx.cloud.callFunction({
										name: 'logins',
										data: {
											useName: 'isregist'
										}
									}).then(res => {
										return res.result
									})
									// console.log("reg:", isReg)
									if (isReg.isregist) {
										uni.setStorageSync('openid', isReg.openid)
										uni.setStorageSync('userinfo', isReg.data.data[0])
										uni.setStorageSync('noReg', true)
									}
									uni.hideLoading()
									uni.switchTab({
										url: "/pages/tabbar/index/index"
									})
									return
								}
								uni.hideLoading()
								uni.showToast({
									title: "注册出错,请联系管理员",
									icon: "error"
								})
							}
						}
					})
				} else {
					uni.showToast({
						title: "有空白项未填写",
						icon: "error"
					})
					return
				}
			},
			radioChange(evt) {
				// console.log(evt, "evt")
				this.registData.auth = evt.detail.value
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.detail.value) {
						this.current = i;
						break;
					}
				}
			},
			isValidChinesePhoneNumber(phoneNumber) {
				return /^1[3-9]\d{9}$/.test(phoneNumber);
			},
		},
		onShow() {
			uni.showToast({
				title: '您未注册',
				icon: 'none',
				duration: 2000,
				success() {
					console.log("成功")
				},
				fail(e) {
					console.log("失败：", e)
				}
			})
		}
	}
</script>

<style lang="scss" scoped>
	.regist {
		padding: $uni-spacing-row-base;
		background: linear-gradient(135deg, #a1c4fd, #a1ffd5);
		min-height: 100vh;

		.text-title {
			text-align: center;
			font-size: $uni-font-size-lg;
			font-weight: 800;

		}

		.reginp {
			border: 1px solid;
			height: 80rpx;
			border-radius: $uni-border-radius-base;
			padding-left: 12rpx;
			margin: $uni-spacing-row-sm 0;
		}

		.regbtn {
			color: #fff;
			background: #0f9efd;
		}
	}
</style>